package com.song.shop.nio.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.charset.Charset;
import java.util.Iterator;

/**
 * @author Song Yinghui
 * @date 2019/2/14
 * @Version 1.0
 */
public class NIOServer {
    public static void start(){
        try {
            //获取channel
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            //非阻塞
            serverSocketChannel.configureBlocking(false);
            //绑定端口
            serverSocketChannel.bind(new InetSocketAddress(8888));
            //获取select
            Selector selector = Selector.open();
            //将channel 注册到select
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

            //获取已就绪的
            while (selector.select() > 0 ){
                System.out.println(selector.select());
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                while (iterator.hasNext()){
                    SelectionKey key = iterator.next();
                    if (key.isValid()){
                        //新连接  注册到select
                        if (key.isAcceptable()){
                            SocketChannel socketChannel = serverSocketChannel.accept();
                            socketChannel.configureBlocking(false);
                            socketChannel.register(selector,SelectionKey.OP_READ);
                        }

                        //可读链接
                        if (key.isReadable()){
                            SocketChannel channel = (SocketChannel) key.channel();
                            ByteBuffer byteBuffer = ByteBuffer.allocate(2048);
                            /*int read = channel.read(byteBuffer);
                            if (read > 0){
                                byteBuffer.flip();
                                byte[] bytes = new byte[byteBuffer.remaining()];
                                byteBuffer.get(bytes);
                                String s = new String(bytes, Charset.forName("utf-8"));
                                System.out.println(s);
                            }*/
                            int len = 0 ;
                            String str = "";
                            while ((len = channel.read(byteBuffer)) > 0){
                                str = new String(byteBuffer.array(), 0, len);
                                System.out.println(str);
                                byteBuffer.clear();
                            }

                            //回复个消息
                            byteBuffer.clear();
                            byte[] bytes = ("我是服务端，消息已收到啦").getBytes("UTF-8");
                            byteBuffer.put(bytes);
                            byteBuffer.flip();
                            channel.write(byteBuffer);
                        }
                    }
                    iterator.remove();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Thread(()-> start()).start();
    }
}
